package ar.com.jwa.services;
import java.util.Collection;
import java.util.Date;

import javax.persistence.EntityManagerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import ar.com.jwa.domain.Game;
import ar.com.jwa.domain.State;
import ar.com.jwa.domain.User;


public class UserLocalServiceTestCase extends ServiceTestCase {

	@Autowired
	private UserLocalService userLocalService;
	
	@Autowired
	public GameLocalService gameLocalService; 
	
	@Autowired
	private EntityManagerFactory entityManagerFactory;

	
	
	public void testGetUserById(){
		
		User user = null;
		try {
			
			user = this.getUserLocalService().getUserById(new Long(1));
			
		} catch (Exception e) {
			fail(e.getMessage());
		}
		
		assertNotNull(user);
	}
	
	
	@Transactional
	public void testGetGamesByState(){
		
		try {
			
			Collection<User> users = this.getUserLocalService().findAll();
			
			for (User user : users) {
				Collection<Game> toConfirmGames = this.getGameLocalService().getGamesByState(State.TO_CONFIRM, user);
				
				for (Game game : toConfirmGames) {
					assertEquals(true, game.getState().equals(State.TO_CONFIRM));
					assertEquals(true, game.getTheDate().after(new Date()));
//					assertEquals(true, game.getPlayers().contains(user));
				}
				
				Collection<Game> confirmedGames = this.getGameLocalService().getGamesByState(State.CONFIRMED, user);
				
				for (Game game : confirmedGames) {
					assertEquals(true, game.getState().equals(State.CONFIRMED));
					assertEquals(true, game.getTheDate().after(new Date()));
//					assertEquals(true, game.getPlayers().contains(user));
				}
				
				Collection<Game> playedGames = this.getGameLocalService().getGamesByState(State.PLAYED, user);
				
				for (Game game : playedGames) {
					assertEquals(true, game.getState().equals(State.PLAYED));
					assertEquals(true, game.getTheDate().before(new Date()));
//					assertEquals(true, game.getPlayers().contains(user));
				}
				
				Collection<Game> suspendedGames = this.getGameLocalService().getGamesByState(State.SUSPENDED, user);
				
				for (Game game : suspendedGames) {
					assertEquals(true, game.getState().equals(State.SUSPENDED));
				}
				
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			fail(e.getCause().getMessage());
		}
		
	}

	
	
	
	/**
	 * @return the userLocalService
	 */
	public UserLocalService getUserLocalService() {
		return userLocalService;
	}

	/**
	 * @param userLocalService the userLocalService to set
	 */
	public void setUserLocalService(UserLocalService userLocalService) {
		this.userLocalService = userLocalService;
	}


	/**
	 * @return the gameLocalService
	 */
	public GameLocalService getGameLocalService() {
		return gameLocalService;
	}


	/**
	 * @param gameLocalService the gameLocalService to set
	 */
	public void setGameLocalService(GameLocalService gameLocalService) {
		this.gameLocalService = gameLocalService;
	}


	/**
	 * @return the entityManagerFactory
	 */
	public EntityManagerFactory getEntityManagerFactory() {
		return entityManagerFactory;
	}


	/**
	 * @param entityManagerFactory the entityManagerFactory to set
	 */
	public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
		this.entityManagerFactory = entityManagerFactory;
	}
	
}
